package com.zyx.javademo.clazz;

import java.lang.reflect.ParameterizedType;

/**
 * @author Yaxi.Zhang
 * @since 2022/11/7 14:36
 */
public abstract class AbstractDao<T> {
    public Class<T> getActualType() {
        ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
        return (Class<T>) parameterizedType.getActualTypeArguments()[0];
    }

    /**
     * 获取局部变量的泛型，注意：泛型必须以匿名内部类的方式
     */
    public static void main(String[] args) {
        AbstractDao<String> dao = new AbstractDao<String>() {};
        System.out.println(dao.getActualType().getTypeName());
    }
}

